import { createNaiveStorage } from 'naive-storage'
import { BaseStorage, getLsKey } from './base'
import { USER_INFO_KEY } from '@/constants/ls'
import { UserInfo } from '@/store/types'

const storage = createNaiveStorage<UserInfo>({
  debug: process.env.NODE_ENV !== 'production',
  storage: BaseStorage
})
const KEY = getLsKey(USER_INFO_KEY)
export function setStorageUserInfo(replacement: UserInfo) {
  storage.set(KEY, replacement)
}
export function getStorageUserInfo() {
  return storage.get(KEY)
}
export function removeStorageUserInfo() {
  storage.del(KEY)
}
export function patchStorageUserInfo(userInfo: Partial<UserInfo>) {
  if (!userInfo || Object.keys(userInfo).length === 0) return
  const prev = getStorageUserInfo()
  if (prev) {
    setStorageUserInfo({
      ...prev,
      ...userInfo
    })
  } else {
    setStorageUserInfo(userInfo)
  }
}
